<template>
	<view class="content">
		<view>我是index</view>
		<view>msg:{{msg}}</view>
		<button @click="toIndex2">跳转到index2</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				msg: '',
				subsTopic: 'sys/b69D7OHdY8P7C6Fp/60:1D:9D:F3:B7:E9/thing/event/property/post/requestOilcan',
				pubtopic: 'sys/b69D7OHdY8P7C6Fp/60:1D:9D:F3:B7:E9/thing/service/requestOilcan'
			}
		},
		onLoad() {
			/**
			 * 1. var mqtt = require('mqtt');
			 * 2. var url = 'xxxxxxx'
			 * 	  var options = {xxxx}
			 * 3. var client = mqtt.connect(url,options);
			 * 4. client.on('connect',()=>{ 回调函数，连接成功时调用 })
			 * 5. client.on('error',(e)=>{ 回调函数，连接失败时调用 })
			 * 6. client.on('reconnect',()=>{ 回调函数，重连时调用 })
			 * 7. client.on('message',(topic,message) => { 回调函数，接收到消息时调用 })
			 * 8. client.on('close',() => {回调函数，连接关闭时调用})
			 * 
			 * 9. client.subscribe(topic); //订阅主题
			 * 10. client.publish(topic,msg); //发布信息
			 * 11. client.unsubscribe(topic); //退订主题
			 * */
			this.btn1();
		},
		onShow() {

		},
		methods: {
			btn1() {
				var that = this;
				this.$store.getters.getState.client.subscribe(that.subsTopic, () => {
					console.log("index | 订阅主题成功");
				});
				this.$store.getters.getState.client.publish(that.pubtopic);

				var timer = setInterval(()=>{
					that.ss();
				},1000);
			},
			ss() {
				var that = this;
				that.msg = that.$store.getters.getState.message;
				console.log("state.msg:", that.$store.getters.getState.message);
			},
			toIndex2() {
				this.$store.getters.getState.client.unsubscribe(this.topic, () => {
					console.log("index | 退订主题成功");
				});
				uni.navigateTo({
					url: '../index2/index2'
				})
			}
		}
	}
</script>

<style>
</style>
